import hilog from '@ohos.hilog';
import http from '@ohos.net.http'

const host = 'http://42.194.132.137:8081'

export const log =function (message){

        for(let i = 0 ; i < arguments.length ; i++){
                let m = arguments[i];
                if (typeof  m === 'object'){
                        m = JSON.stringify(m)
                }
                console.log(m)
                hilog.debug(0,'MY_DEBUG','%{private}s' , m)
        }


}


export const  httpGet = (object) =>{
    const httpRequest = http.createHttp();

        if(!object.url){
                return   log('url不能为空');
        }

         httpRequest.request(host + object.url,
                {
               // method: '',
                header: {
                    'Content-Type': 'application/json'
                },
                },
                (err,data)=>{
//                        if(err){
//                                return log('网络请求错误', err)
//                        }
                        if(data.responseCode !== 200){
                                return log('返回状态码错误,状态码是：' + data.responseCode)
                        }
                    if(typeof object.success === 'function'){
                        let r = data.result
                       try{
                           r = JSON.parse(r)
                       }catch{

                       }
                        object.success(r)
                    }
					httpRequest.destroy();
                }
        )

}

